<template>
  <div class="list-page">
    <Row>
      <div class="toolbar">
        <Input
          placeholder="手机号码"
          v-model="bloc.form.phone"
          style="width: 180px; margin-right: 10px"
        />
        <Button
          type="primary"
          icon="ios-search"
          style="margin-right: 10px"
          size="default"
          @click="bloc.load(1, bloc.paged.pageSize)"
        >查询
        </Button>
        <Button
          icon="md-refresh"
          style="margin-right: 10px"
          size="default"
          @click="bloc.reset()"
        >重置</Button
        >
      </div>
    </Row>
    <Row>
      <Table
        border
        :loading="bloc.loading"
        :columns="columns"
        highlight-row
        :data="bloc.paged.items">
        <slot name="footer" slot="footer">
          <Page
            show-elevator
            show-total
            show-sizer
            :total="bloc.paged.itemCount"
            :current="bloc.paged.pageIndex"
            :page-size="bloc.paged.pageSize"
            :page-size-opts="[15, 30, 50, 100]"
            @on-change="(val) => bloc.load(val, bloc.paged.pageSize)"
            @on-page-size-change="(val) => bloc.load(bloc.paged.pageIndex, val)"
          />
        </slot>
      </Table>
    </Row>
  </div>
</template>

<script lang="ts">
    import { Component, Vue } from 'vue-property-decorator';
    import { Route } from 'vue-router';
    import { ListBloc } from '@/view/coupons/SelectedMemberBloc';
    import { formatDate } from '@/base/util/Dates';
    import { Validation } from '@/base/Validation';
    import { Resources } from '@/base/Resources';
    import { isEmpty } from '@/base/util/Strings';
    import { store } from '@/store/Store';
    import { deepAssign } from '@/base/util/Objects';

    const columns = (bloc: ListBloc) => [
        {
            title: '所属品牌商',
            key: 'companyId',
            render: (h: any, params: any) => {
                return h('span', {
                    domProps: {
                        innerText: bloc.getCompanyName(params.row.companyid)
                    }
                });
            },
            align: 'left',
            minWidth: 100,
            tooltip: true
        },
        {
            title: '所属经销商',
            key: 'agentId',
            render: (h: any, params: any) => {
                return h('span', {
                    domProps: {
                        innerText: bloc.getAgentName(params.row.agentid)
                    }
                });
            },
            align: 'left',
            minWidth: 100,
            tooltip: true
        },
        {
            title: '昵称',
            key: 'nickname',
            align: 'left',
            minWidth: 100,
            tooltip: true
        },
        {
            title: '手机',
            key: 'phone',
            align: 'left',
            minWidth: 100,
            tooltip: true
        },
        {
            title: '生效状态',
            key: 'state',
            align: 'left',
            render: (h: any, params: any) => {
                return h('span', {
                    domProps: {
                        innerText:
                            params.row.state === 0
                                ? '未发放' : params.row.state === 1
                                ? '已发放未使用' : params.row.state === 2
                                ? '已使用' : '已过期'
                    }
                });
            },
            minWidth: 60,
            tooltip: true
        },
        {
            title: '生效时间',
            render: (h: any, params: any) => {
                return h('span', {
                    domProps: {
                        innerText: params.row.updated ? formatDate(
                            new Date(params.row.updated),
                            'yyyy-MM-dd hh:mm:ss'
                        ) : '未发放'
                    }
                });
            },
            width: 220,
            align: 'left',
            tooltip: true
        }
    ];

    @Component({
        components: {}
    })
    export default class extends Vue {
        beforeRouteEnter(to: Route, from: Route, next: (vm: any) => void) {
            next((vm) => {
                vm.bloc.couponId = vm.idx;
                if (vm.idx) {
                    vm.bloc.load(vm.bloc.paged.pageIndex, vm.bloc.paged.pageSize).catch((e) => {
                        vm.$Message.error('获取错误');
                    });
                    vm.bloc.getCompanyList(vm.bloc.companyPage.pageIndex, vm.bloc.companyPage.pageSize, 'ASC');
                    vm.bloc.getAgentList(vm.bloc.angent.pageIndex, vm.bloc.angent.pageSize, 'ASC');
                } else {
                    vm.$Message.error('接收错误');
                }
            });
        }

        bloc: ListBloc = new ListBloc();
        columns = columns(this.bloc);
        Validation = Validation;
        Resources = Resources;

        get tepee() {
            return store.state.user.tepee
        }
        get idx() {
            return Number(this.$route.params.id);
        }
    }
</script>

<style lang="less">
</style>
